﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using A.HTMlServer;

namespace WF.Webdata
{
    public partial class ArticleDownload : Form
    {
        List<HListModel> List = null;
        List<ArticleModel> AList = null;
        object objState =new object();
        public ArticleDownload()
        {
            InitializeComponent();
        }

        private void ArticleDownload_Load(object sender, EventArgs e)
        {
            BDownload.Enabled = false;
            BDownload.Enabled = false;
        }

        private void Binput_Click(object sender, EventArgs e)
        {
            Binput.Enabled = false;
            List=A.HTMlServer.ArticleData.DataHListModel();
            if (List != null)
            {
                Lmessage.Text="导入"+List.Count+"条数据!";
                BDownload.Enabled = true;
            }

        }

        private void BDownload_Click(object sender, EventArgs e)
        {
            pBar.Maximum =List.Count;

            RunDownload();

            Thread th = new Thread(RunPbar);
            th.Start();
        }

        private void RunDownload()
        {
            A.HTMlServer.Control.AddList(List);
        }

        delegate void Dfun();
        private void RunPbar()
        {
            Dfun df=()=>Pbar();
            //this.Invoke(df);
            do
            {
                this.Invoke(df);
                Thread.Sleep(500);
            } while (A.HTMlServer.Control.ActiveState);
            df = () => onBsave();
            this.Invoke(df);
        }

        private void Pbar()
        {
            pBar.Value =A.HTMlServer.Control.List.Count;
            Lmessage.Text = "已下载" + A.HTMlServer.Control.List.Count+"条数据!";
        }

        private void onBsave()
        {
            Bsave.Enabled = true;
        }

        private void Bsave_Click(object sender, EventArgs e)
        {
            AList = A.HTMlServer.Control.List;
            if (AList.Count > 0)
            {
                Thread T;
                for (var i = 0; i < 5;i++)
                {
                    T = new Thread(RunSave);
                    T.Start();
                }

                pBar.Maximum = AList.Count;
                Thread th = new Thread(RunPbarSave);
                th.Start();

                Binput.Enabled = false;
                BDownload.Enabled = false;
            }
        }

        private void PbarSave()
        {
            pBar.Value = pBar.Maximum-AList.Count;
            Lmessage.Text = "已保存" + A.HTMlServer.Control.List.Count + "条数据!";
        }
        private void RunPbarSave()
        {
            Dfun df = new Dfun(PbarSave);
            do
            {
                this.Invoke(df);
                Thread.Sleep(500);
            }while (AList.Count!=0);
            df = new Dfun(onButton);
            this.Invoke(df);
            if (A.HTMlServer.Control.HList.Count > 0)
            {
                string moreid = null;
                A.HTMlServer.Control.HList.ForEach((HL) => { moreid = HL.ID + ","; });
                moreid = moreid.TrimEnd(',');
                A.HTMlServer.ArticleData.DatatoUpdateFlag(moreid);
            }
            else
            {
                A.HTMlServer.ArticleData.DatatoUpdateFlag("-1");
            }
        }
        private void onButton()
        {
            Binput.Enabled = false;
            BDownload.Enabled = false;
        }

        private void RunSave()
        {
            
            do
            {
                lock(objState)
                {
                    if (AList.Count > 0)
                    {
                        A.HTMlServer.ArticleData.DatatoSqlArticle(AList[0]);
                        AList.RemoveAt(0);
                    }
                }
                Thread.Sleep(500);
            }while(AList.Count!=0);
        }
    }
}
